<?php

/**
 * This is the model class for table "{{class}}".
 *
 * The followings are the available columns in table '{{class}}':
 * @property integer $id
 * @property integer $facility_id
 * @property integer $training_id
 * @property string $title
 * @property string $body
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Facility $facility
 * @property Training $training
 * @property Clstudent[] $clstudents
 * @property Classtime[] $classtime
 */
class GuitarClass extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return GuitarClass the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{class}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('facility_id, training_id, start_time, end_time, create_time, update_time, status', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 255),
            array('body', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, facility_id, training_id, title, body, start_time, end_time, create_time, update_time, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'facility' => array(self::BELONGS_TO, 'Facility', 'facility_id'),
            'training' => array(self::BELONGS_TO, 'Training', 'training_id'),
            'students' => array(self::MANY_MANY, 'Students', 'tbl_clstudent(class_id,student_id)'),
            'studentCount' => array(self::STAT, 'Clstudent', 'class_id'),
            'classtime' => array(self::HAS_MANY, 'Classtime', 'class_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'facility_id' => 'Cơ sở',
            'training_id' => 'Khóa học',
            'title' => 'Tiêu đề',
            'body' => 'Nội dung',
            'start_time' => 'Bắt khóa học',
            'end_time' => 'Kết thúc khóa học',
            'create_time' => 'Thời gian tạo',
            'update_time' => 'Thời gian cập nhật',
            'status' => 'Trạng thái',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('facility_id', $this->facility_id);
        $criteria->compare('training_id', $this->training_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('body', $this->body, true);
        $criteria->compare('start_time', $this->start_time);
        $criteria->compare('end_time', $this->end_time);
        $criteria->compare('create_time', $this->create_time);
        $criteria->compare('update_time', $this->update_time);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public static function listFacilities() {
        $facilities = Facility::model()->findAll();
        $items = array();
        if ($facilities) {
            foreach ($facilities as $faci) {
                $items[$faci['id']] = $faci['title'];
            }
        }

        return $items;
    }

    public static function listTraining() {
        $facilities = Training::model()->findAll();
        $items = array();
        if ($facilities) {
            foreach ($facilities as $faci) {
                $items[$faci['id']] = $faci['name'];
            }
        }

        return $items;
    }

    public function beforeSave() {
        if ($this->isNewRecord) {
            $this->create_time = time();
            $this->update_time = time();
        } else {
            $this->update_time = time();
        }
        return parent::beforeSave();
    }

    public function checkRegistered() {
        //Nếu là khách vãng lai chỉ được đăng ký lớp cơ bản
        if (Yii::app()->user->isGuest) {
            if($this->id == 1){
                return false;
            }else{
                return true;
            }
                
        }
        
        if (!Yii::app()->user->isGuest) {
            $user = User::model()->findByPk(Yii::app()->user->id);
            //var_dump($user->training_id);die();
            if ((int)$user->training_id != $this->training->id) {
                $st = $this->students;
                if ($st) {
                    $flag = false;
                    foreach ($st as $s) {

                        if ($s->user_id == Yii::app()->user->id) {
                            $flag = true;
                        }
                    }

                    if ($flag) {
                        return true;
                    } else {
                        $role = true;

                        switch ($this->id) {
                            case 1:
                                $role = Yii::app()->user->checkAccess('reg_basic');
                                break;
                            case 2:
                                $role = Yii::app()->user->checkAccess('reg_advance');
                                break;
                        }

                        return !$role;
                    }
                }
            }else{
                return true;
            }
        }
        return false;
    }

}